import { defineEventHandler, getQuery } from 'h3';

import { usePageResponseSuccess, useResponseError } from '../utils/response';

// 费用与成本分析数据生成
function generateExpensesAnalyticsData(
  vehicles: any[],
  maintenanceRecords: any[],
) {
  const expensesData = [];

  // 为每辆车生成2条数据
  for (const vehicle of vehicles) {
    // 生成随机的总里程（5000-100000公里）
    const totalMileage = Math.round(Math.random() * 95_000 + 5000);

    // 生成随机的维保次数（0-10次）
    const maintainCount = Math.floor(Math.random() * 11);

    // 生成随机的维保总费用（根据维保次数计算）
    const maintainTotalCost =
      Math.round(maintainCount * (Math.random() * 2000 + 500) * 100) / 100;

    // 生成预估燃油费（根据总里程计算，油耗8-12升/百公里，油价6-8元/升）
    const fuelConsumption = Math.random() * 4 + 8; // 8-12升/百公里
    const fuelPrice = Math.random() * 2 + 6; // 6-8元/升
    const estimatedFuelCost =
      Math.round((totalMileage / 100) * fuelConsumption * fuelPrice * 100) /
      100;

    // 计算总成本
    const totalCost =
      Math.round((maintainTotalCost + estimatedFuelCost) * 100) / 100;

    expensesData.push({
      vehicleId: vehicle.vehicleId,
      licensePlate: vehicle.licensePlate,
      totalMileage,
      maintainCount,
      maintainTotalCost,
      estimatedFuelCost,
      totalCost,
    });
  }

  return expensesData;
}

// 模拟车辆数据（符合7位车牌格式）
const mockVehicles = [
  { vehicleId: 30_001, licensePlate: '京A12345' },
  { vehicleId: 30_002, licensePlate: '京B56789' },
  { vehicleId: 30_003, licensePlate: '京C90123' },
  { vehicleId: 30_004, licensePlate: '京D34567' },
  { vehicleId: 30_005, licensePlate: '京E78901' },
  { vehicleId: 30_006, licensePlate: '京F12345' },
  { vehicleId: 30_007, licensePlate: '京G56789' },
  { vehicleId: 30_008, licensePlate: '京H90123' },
  { vehicleId: 30_009, licensePlate: '京J34567' },
  { vehicleId: 30_010, licensePlate: '京K78901' },
];

// 模拟维保记录数据
const mockMaintenanceRecords = [
  { vehicleId: 30_001, cost: 1500 },
  { vehicleId: 30_002, cost: 800 },
  { vehicleId: 30_003, cost: 1200 },
  { vehicleId: 30_004, cost: 2000 },
  { vehicleId: 30_005, cost: 600 },
  { vehicleId: 30_006, cost: 1800 },
  { vehicleId: 30_007, cost: 500 },
  { vehicleId: 30_008, cost: 300 },
  { vehicleId: 30_009, cost: 200 },
  { vehicleId: 30_010, cost: 1000 },
];

// 生成费用与成本分析数据
const expensesAnalyticsData = generateExpensesAnalyticsData(
  mockVehicles,
  mockMaintenanceRecords,
);

// 获取费用与成本分析数据
export const getExpensesAnalyticsHandler = defineEventHandler(async (event) => {
  try {
    const query = getQuery(event);
    const page = Number.parseInt(query.page as string) || 1;
    const pageSize = Number.parseInt(query.pageSize as string) || 20;

    // 分页处理
    const startIndex = (page - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const paginatedData = expensesAnalyticsData.slice(startIndex, endIndex);

    return usePageResponseSuccess(page, pageSize, paginatedData, {
      message: '获取成功',
    });
  } catch {
    return useResponseError('获取失败');
  }
});

// 获取费用与成本分析图表数据
export const getExpensesChartHandler = defineEventHandler(async (_event) => {
  try {
    // 生成饼图数据（按车辆状态分布）
    const pieChartData = [
      { name: '可用', value: 15 },
      { name: '维保中', value: 10 },
      { name: '已报废', value: 5 },
    ];

    // 生成柱状图数据（按月份的维保费用）
    const barChartData = [
      { month: '1月', cost: 15_000 },
      { month: '2月', cost: 18_000 },
      { month: '3月', cost: 12_000 },
      { month: '4月', cost: 20_000 },
      { month: '5月', cost: 16_000 },
      { month: '6月', cost: 14_000 },
    ];

    return useResponseSuccess({
      pieChart: pieChartData,
      barChart: barChartData,
    });
  } catch {
    return useResponseError('获取失败');
  }
});
